package com.supreme.threadserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.threadserver.service.AqsUtilService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ArrayBlockingQueue;

@RestController
@RequestMapping(value = "aqs-util")
public class AqsUtilController {

    private static final ArrayBlockingQueue<Thread> loopThreadBlockingQueue = new ArrayBlockingQueue<>(5);

    @Resource
    private AqsUtilService aqsUtilService;

//    @PostConstruct
    public void consumerBlockingQueueThread() {
        aqsUtilService.startLoopThreadBlockQueueConsumer();
    }

    @GetMapping(value = "startLoopThreadConsumer")
    public AjaxResp startLoopThreadConsumer() {
        aqsUtilService.startLoopThreadBlockQueueConsumer();
        return AjaxResp.instance();
    }

    @GetMapping(value = "stopLoopThreadConsumer")
    public AjaxResp stopLoopThreadConsumer() {
        aqsUtilService.stopLoopThreadBlockQueueConsumer();
        return AjaxResp.instance();
    }

    @GetMapping(value = "putLoopThread")
    public AjaxResp putLoopThread(@RequestParam(value = "loopCount") Integer loopCount,
                                   @RequestParam(value = "timeout") Long timeout) {
        int result = aqsUtilService.putLoopThread(loopCount, timeout);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "takeLoopThread")
    public AjaxResp takeLoopThread() {
        aqsUtilService.takeLoopThread();
        return AjaxResp.instance();
    }
}
